#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QAction>
#include <QMenuBar>
#include <QMenu>
#include <QDebug>
#include <QFile>
#include <QErrorMessage>
#include <QVariant>

#include "sconst.h"
#include "qtabextwidget.h"
#include "menubuilder.h"
#include "qtoolbaraction.h"
#include "qtoolbarview.h"
#include "databaseinstancemngr.h"
#include "qmysqldabasemngr.h"
#include "statusbar.h"
#include "errorcodesview.h"
#include "errorcodesedit.h"

#include "dbquerymodulelist.h"


MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
                                          ui(new Ui::MainWindow)
{
  centralWidget         = NULL;
  pErrorCodesEditWidget = NULL;
  pErrorCodesViewWidget = NULL;
  pDatabaseIpDalog      = NULL;

  ui->setupUi(this);
  showMaximized();

  setWindowTitle(SC_TITLE);
  initMenu();
  initToolBar();
  initTabWidget();
  (void*)StatusBar::getStatusBar(this); //init status bar
}

void MainWindow::initMenu()
{
  MenuBuilder *pMenuBuilder = new MenuBuilder(this);
  setMenuBar(pMenuBuilder);
}

void MainWindow::initToolBar()
{
  addToolBar(new QToolBarAction(this));
  addToolBar(new QToolBarView(this));
}

void MainWindow::initTabWidget()
{
  if(centralWidget == NULL)
    centralWidget = new QTabExtWidget(this);

//  QString strBackgroundStyle = "QWidget { background: lightgray; }";
  QString strBackgroundStyle = "QWidget { background-image: url(" + QString(SC_IMG_MAIN_FRAME_BACKGROUND) + "); }";
  QString strStyleSheet      = "QTabBar::tab { background: gray; color: white; padding: 10px; } "
                               "QTabBar::tab:selected { background: lightgray; } "
                               "QTabWidget::pane { border: 0; } "
                               + strBackgroundStyle;

  centralWidget->setStyleSheet(strStyleSheet);

/*
  centralWidget->addTab(new QWidget(),"TAB 1");
  centralWidget->addTab(new QWidget(),"TAB 2");
  centralWidget->addTab(new QWidget(),"TAB 3");
*/
  setCentralWidget(centralWidget);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::signalOpenErrorCodeEditTable()
{
  int tabIndex = -1;
  if(pErrorCodesEditWidget == NULL) {
    pErrorCodesEditWidget = (ErrorCodesView*)new ErrorCodesEdit(this);
    pErrorCodesEditWidget->createPanel();
  }

  tabIndex = centralWidget->addTab(pErrorCodesEditWidget, SC_ERROR_CODE_EDIT);
  centralWidget->setCurrentIndex(tabIndex);
}

void MainWindow::signalOpenErrorCodeViewTable()
{
  int tabIndex = -1;
  if(pErrorCodesViewWidget == NULL) {
    pErrorCodesViewWidget = (ErrorCodesView*)new ErrorCodesView(this);
    pErrorCodesViewWidget->createPanel();
  }
  tabIndex = centralWidget->addTab(pErrorCodesViewWidget, SC_ERROR_CODE_VIEW);



#if 0
  if(pErrorCodesViewWidget == NULL) {
    pErrorCodesViewWidget = (ErrorCodesWidget*)new ErrorCodesWidget(this);
/* connection to the DB MUST be created */
    DataBaseInstanceMngr *pdbinstmngr = DataBaseInstanceMngr::getDataBaseInstanceMngr();
    DatabaseManager *pdbmngr = pdbinstmngr->getDatabaseManager(SC_ERROR_CODE_DB_NAME);
    mysqlquery::dbquerymodulelist l_query;
    ListQVariant lv = pdbmngr->query(l_query.getSqlQuery());

    pErrorCodesViewWidget->setModuleList(l_query.getModuleList(lv));
    pErrorCodesViewWidget->buildWidget();
  }

  tabIndex = centralWidget->addTab(pErrorCodesViewWidget, SC_ERROR_CODE_VIEW);
#endif
  centralWidget->setCurrentIndex(tabIndex);
}

void MainWindow::signalLoginDataBase()
{
  qDebug() << "signalLoginDataBase()";
  if(pDatabaseIpDalog == NULL) {
      QList<QString> db_list;
      db_list.push_back("home_project_test");
      db_list.push_back("test");
      db_list.push_back("test 3");
      db_list.push_back("test 4");
      pDatabaseIpDalog = new DatabaseIpDalog(this);
      pDatabaseIpDalog->buildWidget(db_list, db_list.at(0),
                                    "test",
                                    "test",
                                    "localhost",
                                    3306);
      connect(pDatabaseIpDalog, SIGNAL(dbloginaccept(db_connect)), this, SLOT(signalDbLoginAccept(db_connect)));
      pDatabaseIpDalog->exec();
  } else
    pDatabaseIpDalog->exec();
}

void MainWindow::signalDbLoginAccept(db_connect dbConnect)
{
  DataBaseInstanceMngr *pdbinstmngr = DataBaseInstanceMngr::getDataBaseInstanceMngr();
  DatabaseManager *pdbmngr = pdbinstmngr->getDatabaseManager(SC_ERROR_CODE_DB_NAME);

  if(pdbmngr == 0) {
    pdbmngr = pdbinstmngr->setDatabaseManager(std::string(SC_ERROR_CODE_DB_NAME), REGISTER(QMySqlDatabaseManager));
  }

  qDebug()<< "db login: " << dbConnect.database_login <<
            "db_password: " << dbConnect.database_pswd <<
            "db_name: " << dbConnect.database_name <<
            "db host: " << dbConnect.addr <<
            "db_connect_port: " << dbConnect.port;

  bool ret_value = openDatabase(pdbmngr, dbConnect);

  qDebug()<< "open database result: "<< ret_value;

  if(ret_value == false) {
    QErrorMessage errorMessage;
    errorMessage.setWindowTitle("Error");
    errorMessage.showMessage(pdbmngr->getLastError());
//   qDebug() << pdbmngr->getLastError();
    return;
  }

}

bool MainWindow::openDatabase(DatabaseManager *pDBManager, const db_connect &dbConnect)
{
  pDBManager->setHostname(dbConnect.addr.toStdString());
  pDBManager->setPort(dbConnect.port);
  pDBManager->setUserName(dbConnect.database_login.toStdString());
  pDBManager->setPassword(dbConnect.database_pswd.toStdString());
  pDBManager->setDatabaseName(dbConnect.database_name.toStdString());

  return pDBManager->openDatabase();
}


void MainWindow::signalExit()
{
  DataBaseInstanceMngr *pdbinstmngr = DataBaseInstanceMngr::getDataBaseInstanceMngr();
  DatabaseManager *pdbmngr = pdbinstmngr->getDatabaseManager("errorcodes");
  pdbmngr->closeDatabase();

  QCoreApplication::exit(0);
}
